<?php
declare (strict_types = 1);

namespace app\admin\controller;

use app\BaseController;
use app\common\model\Post\Post as PostModel;

class Post extends BaseController
{
    protected $middleware = [
        \app\admin\middleware\LoginAuth::class
    ];
    // 帖子列表
    public function index()
    {
        $menu = [
            'name' => '问答管理',
            'title' => '帖子列表'
        ];
        $searchfield = $this->request->param('search_field', 'title');
        $keyword = $this->request->param('keyword');
        // halt($param);
        $where = [];
        if ($searchfield && $keyword) {
            $where[] = [$searchfield, 'like', "%$keyword%"];
        }
        // 每页显示数量
        $num = 8;
        $PostList = PostModel::withjoin(['type','user'])->where($where)->order('createtime','desc')->paginate(['list_rows' => $num, 'query' => request()->param()]);
        // 当前页
        $page = $PostList->currentPage();
        return view('', [
            'menu' => $menu,
            'PostList' => $PostList,
            'num' => $num,
            'page' => $page,
            'searchfield' => $searchfield,
            'keyword' => $keyword
        ]);
    }
    // 删除帖子
    public function del(){
        if ($this->request->isAjax()) {
            $action = $this->request->param('action');
            $ids = $this->request->param('ids');
            $postid = $this->request->param('postid');
            // 软删除
            if ($action == 'del') {
                $User = PostModel::find($postid);
                if (!$User) {
                    $this->error('帖子不存在');
                }
                $result = PostModel::destroy($postid);
                if ($result === false) {
                    $this->error('删除失败');
                } else {
                    $this->success('删除成功');
                }
            }
            // 批量软删除
            if ($action == 'batchDelete') {
                $PostList = PostModel::whereIn('id', $ids)->select();
                foreach ($PostList as $item) {
                    if (!in_array($item['id'], $ids)) {
                        $this->error("所选帖子{$item['title']}不存在,请重新选择");
                    }
                }
                $result = PostModel::destroy(['id' => $ids]);

                if ($result !== FALSE) {
                    $this->success('删除成功');
                } else {
                    $this->error("删除失败");
                }
            }
            //单条真实删除
            if ($action == 'Del') {
                $Post = PostModel::onlyTrashed()->find($postid);
                if (!$Post) {
                    $this->error('帖子不存在');
                }
                $result = PostModel::destroy($postid, true);
                if ($result === false) {
                    $this->error('删除失败');
                } else {
                    $this->success('删除成功');
                }
            }
            if ($action == 'BatchDelete') {
                // 查询选中的数据 开启软删除后普通查询不出软删除字段数据 onlyTrashed=>仅查询软删除数据 withTrashed=>所有数据可查
                $PostList = PostModel::onlyTrashed()->whereIn('id', $ids)->select();
                foreach ($PostList as $item) {
                    if (!in_array($item['id'], $ids)) {
                        $this->error("所选帖子{$item['title']}不存在,请重新选择");
                    }
                }
                $result = PostModel::destroy($ids, true);
                if ($result !== FALSE) {
                    $this->success('批量删除成功');
                } else {
                    $this->error("批量删除失败");
                }
            }
        }
    }
    // 回收站
    public function recycle(){
        $menu = [
            'name' => '问答管理',
            'title' => '回收站'
        ];
        $searchfield = $this->request->param('search_field', 'title');
        $keyword = $this->request->param('keyword');
        $where = [];
        if ($searchfield && $keyword) {
            $where[] = [$searchfield, 'like', "%$keyword%"];
        }
        // 每页显示数量
        $num = 8;
        $PostList = PostModel::onlyTrashed()->withjoin(['type','user'])->where($where)->order('createtime','desc')->paginate(['list_rows' => $num, 'query' => request()->param()]);
        // 当前页
        $page = $PostList->currentPage();
        return view('', [
            'menu' => $menu,
            'PostList' => $PostList,
            'num' => $num,
            'page' => $page,
            'searchfield' => $searchfield,
            'keyword' => $keyword
        ]);
    }
    // 帖子恢复
    public function recovery(){
        if($this->request->isAjax()){
            $action = $this->request->param('action');
            $id = $this->request->param('postid');
            $ids = $this->request->param('ids');
            // 单条恢复
            if ($action == 'recovery') {
                $User = PostModel::withTrashed()->find($id);
                if (!$User) {
                    $this->error('帖子不存在');
                }
                $result = $User->restore();
                if ($result) {
                    $this->success('恢复成功');
                } else {
                    $this->error('恢复失败');
                }
            }
            // 批量恢复
            if ($action == 'BatchRecovery') {
                // 查询选中的数据
                $UserList = PostModel::withTrashed()->whereIn('id', $ids)->select();
                foreach ($UserList as $item) {
                    if (!in_array($item['id'], $ids)) {
                        $this->error("所选{$item['title']}不存在,请重新选择");
                    }
                }
                $result = PostModel::onlyTrashed()->where(['id' => $ids])->update(['deletetime' => null]);

                if ($result !== FALSE) {
                    $this->success('批量恢复成功');
                } else {
                    $this->error("批量恢复失败");
                }
            }
        }
    }
}
